<%@page contentType="text/html" pageEncoding="windows-1252"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.ResultSetMetaData"%>
<%@page import="java.sql.Statement"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.transaction.UserTransaction"%>
<%@page import="org.json.JSONWriter"%>

<%
String query = request.getParameter("query");
String datasourceName = (String)session.getAttribute("datasourceName");
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup(datasourceName);
UserTransaction ut = (UserTransaction)ic.lookup("java:comp/UserTransaction");
ut.begin();
Connection conn = null;
try {
    conn = ds.getConnection();
    Statement st = conn.createStatement();
    System.out.println(query);
    //st.setEscapeProcessing(false);
    
    boolean isItResultSet = st.execute(query.trim());
    ResultSet rs = null;    
    int updateCount = -1;
    rs = st.getResultSet();
    updateCount = st.getUpdateCount();

    JSONWriter jw = new JSONWriter(out);
    jw.array();
    while(rs != null || updateCount != -1) {
        jw.object();
        jw.key("updateCount");
        jw.value(updateCount);
        if(rs != null) {
            ResultSetMetaData md = rs.getMetaData();
            jw.key("columnDetails");
            jw.array();
            for(int j=1; j<=md.getColumnCount(); j++) {
                jw.value(md.getColumnLabel(j));
            }
            jw.endArray();
            jw.key("data");
            jw.array();
            int i=0;
            while(rs.next()) {
                jw.object();
                for(int j=1; j<=md.getColumnCount(); j++) {
                    jw.key(md.getColumnLabel(j));
                    jw.value(rs.getObject(j));
                }
                jw.endObject();
                i++;
            }
            jw.endArray();
        }
        jw.endObject();
            /*
        if(isItResultSet) {
            System.out.println("========= In RS =====");
            //set columnDetails
            //set data
            ResultSetMetaData rsMd = rs.getMetaData();
            for(int i=1; i<=rsMd.getColumnCount(); i++) {
                System.out.println("=========" + rsMd.getColumnLabel(i));
            }
            int i=0;
            while(rs.next()) {
                for(int j=1; j<=rsMd.getColumnCount(); j++) {
                    System.out.println("=========" + rs.getObject(j));
                }
                i++;
            }
        } else {
            System.out.println("=========" + updateCount);
        }*/

        isItResultSet = st.getMoreResults();
        rs = st.getResultSet();
        updateCount = st.getUpdateCount();
    }
    jw.endArray();
    //we only write o/p of the last statement ...
    ut.commit();
}
catch(Throwable th) {
    th.printStackTrace();
    if(ut != null) {
        try {ut.rollback();}catch(Exception ex){ex.printStackTrace();}
    }
    throw th;
}
finally {
    if(conn != null) {
        try {conn.close();}catch(Exception ex){ex.printStackTrace();}
    }
}
%>
